<!--
 * @Descripttion: 
 * @version: 
 * @Author: wl
 * @Date: 2020-07-03 14:27:42
 * @LastEditors: wl
 * @LastEditTime: 2020-07-06 16:51:51
-->
<template>
  <div class="chartNum">
    <div class="count">过去24小时通行：</div>

    <div class="box-item">
      <li
        :class="{ 'number-item': !isNaN(item), 'mark-item': isNaN(item) }"
        v-for="(item, index) in orderNum"
        :key="index"
      >
        <span v-if="!isNaN(item)">
          <i ref="numberItem">0123456789</i>
        </span>
        <!-- <span class="comma" v-else>{{item}}</span> -->
      </li>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    CID: {
      type: String,
      default: "",
    },
  },

  data() {
    return {
      orderNum: ["0", "0", ",", "0", "0", "0", ",", "0", "0", "0"], // 默认订单总数
      todayAccount: "",
    };
  },
  mounted() {
    this.getCount();

    // 这里输入数字即可调用
  },
  watch: {
    todayAccount() {
      this.timer();
    },
    CID() {
      this.getCount();
    },
  },
  methods: {
    timer() {
      return setTimeout(() => {
        this.getCount();
      }, 5000);
    },

    //得到刷脸数据
    getCount() {
      var param = {
        CID: this.CID,
      };
      var params = JSON.stringify(param);

      this.$api.home.handleDay(params).then((res) => {
        if (res.code == 0) {
          this.todayAccount = res.data.day;
          this.toOrderNum(this.todayAccount);
        } else {
          this.$message({
            message: "操作失败, " + res.msg,
            type: "error",
          });
        }
      });
    },
    // 设置文字滚动
    setNumberTransform() {
      const numberItems = this.$refs.numberItem; // 拿到数字的ref，计算元素数量
      const numberArr = this.orderNum.filter((item) => !isNaN(item));
      // 结合CSS 对数字字符进行滚动,显示订单数量
      for (let index = 0; index < numberItems.length; index++) {
        const elem = numberItems[index];
        elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`;
      }
    },
    // 处理总订单数字
    toOrderNum(num) {
      num = num.toString();
      // 把订单数变成字符串
      if (num.length < 8) {
        num = "0" + num; // 如未满八位数，添加"0"补位
        this.toOrderNum(num); // 递归添加"0"补位
      } else if (num.length === 8) {
        // 订单数中加入逗号
        num = num.slice(0, 2) + "," + num.slice(2, 5) + "," + num.slice(5, 8);
        this.orderNum = num.split(""); // 将其便变成数据，渲染至滚动数组
      } else {
        // 订单总量数字超过八位显示异常
        this.$message.warning("订单总量数字过大，显示异常，请联系客服");
      }
      this.setNumberTransform();
    },
  },

  destroyed() {
    clearTimeout(this.timer);
  },
};
</script>
<style scoped lang='scss'>
/*订单总量滚动数字设置*/
.chartNum {
  // width: 100%;
  display: flex;
  justify-content: center;
  justify-items: center;
  .count {
    font-size: 16px;
    line-height: 100px;
    margin-top: 3%;
  }
}

.box-item {
  position: relative;
  margin: 45px 0px;
  // height: 100px;
  // width: 100%;
  font-size: 16px;
  line-height: 41px;
  vertical-align: middle;
  list-style: none;
  color: #00ffff;
  writing-mode: vertical-lr;
  text-orientation: upright;
  /*文字禁止编辑*/
  -moz-user-select: none;
  /*火狐*/
  -webkit-user-select: none;
  /*webkit浏览器*/
  -ms-user-select: none;
  /*IE10*/
  -khtml-user-select: none;
  /*早期浏览器*/
  user-select: none;
  /* overflow: hidden; */
}

/* 默认逗号设置 */
.mark-item {
  // width: 10px;
  height: 100px;
  margin-right: 5px;
  line-height: 10px;
  font-size: 48px;
  position: relative;

  & > span {
    position: absolute;
    width: 100%;
    bottom: 0;
    writing-mode: vertical-rl;
    text-orientation: upright;
  }
}

/*滚动数字设置*/
.number-item {
  width: 20px;
  height: 32px;
  list-style: none;
  margin-right: 5px;
  background: #2f5794;
  border-radius: 4px;

  & > span {
    position: relative;
    display: inline-block;
    margin-right: 10px;
    width: 100%;
    height: 100%;
    writing-mode: vertical-rl;
    text-orientation: upright;
    overflow: hidden;

    & > i {
      font-style: normal;
      position: absolute;
      top: 9px;
      left: 24%;
      transform: translate(-50%, 0);
      transition: transform 1s ease-in-out;
      letter-spacing: 10px;
    }
  }
}

.number-item:last-child {
  margin-right: 0;
}
</style>
